package com.zz.parkingsystem.service.impl;

import com.zz.parkingsystem.entity.*;
import com.zz.parkingsystem.mapper.AccountMapper;
import com.zz.parkingsystem.mapper.FundFlowMapper;
import com.zz.parkingsystem.mapper.OrderItemMapper;
import com.zz.parkingsystem.mapper.OrderMapper;
import com.zz.parkingsystem.service.*;
import com.zz.parkingsystem.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private ParkingService parkingService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private StrategyService strategyService;
    @Autowired
    private FundFlowMapper fundFlowMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Order record) {
        return orderMapper.insert(record);
    }

    @Override
    public int insertSelective(Order record) {
        return 0;
    }

    @Override
    public int insertSelectiveUser(Order record) {
        //对应账户订单数量加1
        OrderItem orderItem = orderItemMapper.selectByPrimaryKey(record.getOiId().longValue());
        Account account = accountMapper.selectByUserId(orderItem.getUserId());
        account.setAccOrderCount(account.getAccOrderCount()+1);
        accountMapper.updateByPrimaryKeySelective(account);
        return orderMapper.insertSelective(record);
    }

    @Override
    public Order selectByPrimaryKey(Long id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    //用户支付订单，修改订单状态为已支付，订单添加支付时间，支付方式，生成对应的资金流水
    public int updateByPrimaryKeySelectiveUser(Order record) {
        record.setOrderStatus(2);
        record.setPayingTime(new Date());
        record.setPtId(0);
        //订单对应的订单项
        OrderItem orderItem = orderItemMapper.selectByPrimaryKey(record.getOiId().longValue());
        //生成对应的资金流水
        FundFlow fundFlow = new FundFlow();
        fundFlow.setOiId(record.getOiId());
        fundFlow.setPaidType(record.getPtId());
        fundFlow.setFundType(0);
        fundFlow.setOperatorAmount(orderItem.getOiAmount());
        fundFlow.setOperatorTime(new Date());
        fundFlowMapper.insertSelective(fundFlow);
        //将生成的资金流水表id放入对应的订单项表中
        FundFlow fundFlowNew = fundFlowMapper.selFundFlowByOiId(record.getOiId());
        orderItem.setAmoId(fundFlowNew.getFfId());
        orderItemMapper.updateByPrimaryKeySelective(orderItem);
        //将对应的账户表消费金额加，未支付金额减
        Account account = accountMapper.selectByUserId(orderItem.getUserId());
        account.setAccUnpaid(account.getAccUnpaid().subtract(orderItem.getOiAmount()));
        account.setAccSpend(account.getAccSpend().add(orderItem.getOiAmount()));
        accountMapper.updateByPrimaryKeySelective(account);

        return orderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Order record) {
        return orderMapper.updateByPrimaryKey(record);
    }

    @Override
    //通过泊位id，查是否有未未完成的订单（也就是是否该泊位上有车）
    public Order selOrderByStatusByPkId(Integer pkId) {
        return orderMapper.selOrderByStatusByPkId(pkId);
    }

    @Override
//    用户认领订单后，展示在前端的内容
    public OrderVO selOrderVOByOiNo(String oiNo) {
//        OrderVO orderVO = new OrderVO();
//        //订单项id查订单项
//        OrderItem orderItem = (OrderItem) orderItemMapper.selOrderItemByOiNo(oiNo);
//        //VO设置泊位编号
//        orderVO.setpkNo(parkingService.selectByPrimaryKey(orderItem.getPkId().longValue()).getPkNo());
//        //VO设置路段名称
//        orderVO.setSecName(sectionService.selectByPrimaryKey(orderItem.getSecId().longValue()).getSecName());
//        //VO设置收费规则
//        orderVO.setStrategy(strategyService.selectByPrimaryKey(sectionService.selectByPrimaryKey(orderItem.getSecId().longValue()).getStagyId().longValue()));
//        //VO设置驶入时间
//        orderVO.setStartTime(orderItem.getOiStartTime());
//        return orderVO;
        return null;
    }

    @Override
    //订单项id查订单
    public Order selOrderByOrderItemId(Integer orderItemId) {
        return orderMapper.selOrderByOrderItemId(orderItemId);
    }

    @Override
    public Order selOrderByOiNo(String oiNo) {
        OrderItem orderItem = orderItemMapper.selOrderItemByOiNo(oiNo);
        Order order = orderMapper.selOrderByOrderItemId(orderItem.getOiId());
        return order;
    }

    @Override
    public Order selectOiid(Long id) {
        return orderMapper.selectOiid(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return orderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Order> getselectByAllStatus() {
        return orderMapper.selectByAllStatus();
    }
}
